/**
 * Created by zf233 on 2020/2/10
 */
 window.onload = function () {
    var height_total = $(document).height();
    var widht_total = $(document).width();
    var head_title_height = $("#head_title").height();
    var foot_title_height = $("#foot_title").height();
    // var table_head_height = $(".table_head:first").height();
    var menu_div_height = $("#menu_div").height();
    var height = height_total - (head_title_height + foot_title_height) - 10;
    var table_height = height_total - (head_title_height + foot_title_height + menu_div_height) - 50;
    var share_widht = $("#share_form").width();
    var share_heitgh = $("#share_form").height();
    var upload_widht = $("#upload_form").width();
    var upload_heitgh = $("#upload_form").height();
    var update_widht = $("#update_form").width();
    var update_heitgh = $("#update_form").height();
    var move_widht = $("#move_form").width();
    var move_heitgh = $("#move_form").height();
    var archive_widht = $("#archive_form").width();
    var archive_heitgh = $("#archive_form").height();
    var left = (widht_total / 2) - (share_widht / 2) + 'px';
    var top = (height_total / 2) - (share_heitgh / 2) + 'px';
    var upload_left = (widht_total / 2) - ((upload_widht + 40) / 2) + 'px';
    var upload_top = (height_total / 2) - (upload_heitgh / 2) + 'px';
    var update_left = (widht_total / 2) - ((update_widht + 40) / 2) + 'px';
    var update_top = (height_total / 2) - (update_heitgh / 2) + 'px';
    var move_left = (widht_total / 2) - ((move_widht + 40) / 2) + 'px';
    var move_top = (height_total / 2) - (move_heitgh / 2) + 'px';
    var archive_left = (widht_total / 2) - ((archive_widht + 40) / 2) + 'px';
    var archive_top = (height_total / 2) - (archive_heitgh / 2) + 'px';
    $("#share_form").css("left", left);
    $("#share_form").css("top", top);
    $("#upload_form").css("left", upload_left);
    $("#upload_form").css("top", upload_top);
    $("#update_form").css("left", update_left);
    $("#update_form").css("top", update_top);
    $("#move_form").css("left", move_left);
    $("#move_form").css("top", move_top);
    $("#archive_form").css("left", archive_left);
    $("#archive_form").css("top", archive_top);
    $(".center_detail").height(height);
    $("table tbody").height(table_height);
}
window.onresize = function () {
    var height_total = $(document).height();
    var widht_total = $(document).width();
    var head_title_height = $("#head_title").height();
    var foot_title_height = $("#foot_title").height();
    // var table_head_height = $(".table_head:first").height();
    var menu_div_height = $("#menu_div").height();
    var height = height_total - (head_title_height + foot_title_height) - 10;
    var table_height = height_total - (head_title_height + foot_title_height + menu_div_height) - 50;
    var share_widht = $("#share_form").width();
    var share_heitgh = $("#share_form").height();
    var upload_widht = $("#upload_form").width();
    var upload_heitgh = $("#upload_form").height();
    var update_widht = $("#update_form").width();
    var update_heitgh = $("#update_form").height();
    var move_widht = $("#move_form").width();
    var move_heitgh = $("#move_form").height();
    var archive_widht = $("#archive_form").width();
    var archive_heitgh = $("#archive_form").height();
    var left = (widht_total / 2) - (share_widht / 2) + 'px';
    var top = (height_total / 2) - (share_heitgh / 2) + 'px';
    var upload_left = (widht_total / 2) - ((upload_widht + 40) / 2) + 'px';
    var upload_top = (height_total / 2) - (upload_heitgh / 2) + 'px';
    var update_left = (widht_total / 2) - ((update_widht + 40) / 2) + 'px';
    var update_top = (height_total / 2) - (update_heitgh / 2) + 'px';
    var move_left = (widht_total / 2) - ((move_widht + 40) / 2) + 'px';
    var move_top = (height_total / 2) - (move_heitgh / 2) + 'px';
    var archive_left = (widht_total / 2) - ((archive_widht + 40) / 2) + 'px';
    var archive_top = (height_total / 2) - (archive_heitgh / 2) + 'px';
    $("#share_form").css("left", left);
    $("#share_form").css("top", top);
    $("#upload_form").css("left", upload_left);
    $("#upload_form").css("top", upload_top);
    $("#update_form").css("left", update_left);
    $("#update_form").css("top", update_top);
    $("#move_form").css("left", move_left);
    $("#move_form").css("top", move_top);
    $("#archive_form").css("left", archive_left);
    $("#archive_form").css("top", archive_top);
    $(".center_detail").height(height);
    $("table tbody").height(table_height);
}

var move_parentId = -1;
var lastFolderName = "根目录"
$(function () {

    function isEmpty(obj) {
        if (typeof obj == "undefined" || obj == null || obj == "") {
            return true;
        } else {
            return false;
        }
    }

    var clear_left_menu_back = function () {
        $("#all").click();
    }

    var absolutePath = new Vue({
        el: "#absolutePath_li",
        data: {
            absolutePath: []
        },
        methods: {
            open_folder: function (parentid) {
                getList(parentid, null, null);
            }
        }
    })
    var fileItemsTable = new Vue({
        el: '#file_table',
        data() {
            return {
                files: [],
                recycle: 0
            }
        },
        methods: {
            getClassTr: function (isFolder) {
                if (isFolder == 0) {
                    return "fileItem"
                } else {
                    return "folderItem"
                }
            },
            getClassCheckBox: function (isFolder) {
                if (isFolder == 0) {
                    return "fileCheckBox"
                } else {
                    return "folderCheckBox"
                }
            },
            getClassA: function (isFolder) {
                if (isFolder == 0) {
                    return "share_A"
                } else {
                    return "open_A"
                }
            },
            getEvent: function (isFolder, fileId, fileName) {
                if (this.recycle == 0) {
                    this['fun' + isFolder](fileId, fileName)
                }
            },
            fun0: function (fileId, fileName) {
                open_share_form(fileId, fileName);
            },
            fun1: function (fileId) {
                getList(fileId, null, null);
            },
            setRecycle(value) {
                this.recycle = value;
            },
            setFiles(files) {
                this.files = files
            },
            update(fileId) {
                update_fun(fileId)
            },
            choice_tatal() {
                choice_tatal()
            }
        }
    });

    var getList = function (parentid, sortFlag, sortType) {
        $("#remove_add_row").click();
        fileItemsTable.recycle = 0;
        $.ajax({
            async: true,
            url: "/file/browse/list",
            data: {
                parentid: parentid,
                sortFlag: sortFlag,
                sortType: sortType
            },
            dataType: "json",
            success: function (data) {
                if (data.status == 200) {
                    toastr.options = {
                        "positionClass": "toast-top-title-center",
                    };
                    toastr.info(data.msg);
                    fileItemsTable.setFiles(data.data);
                    absolutePath.absolutePath = data.absolutePath;
                    choice_tatal();
                    clear_choice();
                    clear_left_menu_back();
                } else {
                    toastr.options = {
                        "positionClass": "toast-top-title-center",
                    };
                    toastr.error(data.msg);
                }
            },
            error: function (data) {
                toastr.options = {
                    "positionClass": "toast-top-title-center",
                };
                toastr.error("获取文件列表失败");
            },
            type: "post"
        });
    }

    var getListClassify = function (classify) {
        fileItemsTable.recycle = 0;
        $("#remove_add_row").click();
        $.ajax({
            async: true,
            url: "/file/browse/classify",
            data: {
                classify: classify
            },
            dataType: "json",
            success: function (data) {
                if (data.status == 200) {
                    toastr.options = {
                        "positionClass": "toast-top-title-center",
                    };
                    toastr.info(data.msg);
                    fileItemsTable.setFiles(data.data);
                    absolutePath.absolutePath = null;
                    choice_tatal();
                    clear_choice();
                } else {
                    toastr.options = {
                        "positionClass": "toast-top-title-center",
                    };
                    toastr.error(data.msg);
                }
            },
            error: function (data) {
                toastr.options = {
                    "positionClass": "toast-top-title-center",
                };
                toastr.error("获取文件列表失败");
            },
            type: "post"
        });
    }

    var getListRecycle = function (classify) {
        fileItemsTable.recycle = 1;
        $("#remove_add_row").click();
        $.ajax({
            async: true,
            url: "/file/browse/recycle/list",
            dataType: "json",
            success: function (data) {
                if (data.status == 200) {
                    toastr.options = {
                        "positionClass": "toast-top-title-center",
                    };
                    toastr.info(data.msg);
                    fileItemsTable.setFiles(data.data);
                    absolutePath.absolutePath = null;
                    choice_tatal();
                    clear_choice();
                } else {
                    toastr.options = {
                        "positionClass": "toast-top-title-center",
                    };
                    toastr.error(data.msg);
                }
            },
            error: function (data) {
                toastr.options = {
                    "positionClass": "toast-top-title-center",
                };
                toastr.error("获取文件列表失败");
            },
            type: "post"
        });
    }

    var countdown;

    function timeout() {
        count = 30;
        clearInterval(countdown);
        countdown = setInterval(CountDown, 1000);

        function CountDown() {
            $("#acceptAgreeSpan").text("同意" + "（" + count + "）");
            if (count == 0) {
                $("#acceptAgreeSpan").text("同意");
                clearInterval(countdown);
            }
            count--;
        }
    }

    var getCurrentUser = function () {
        $.ajax({
            async: true,
            url: "/user/browse/current/user/get",
            dataType: "json",
            success: function (data) {
                if (data.status == 200) {
                    $("#user_head").attr("src", data.data.headUrl).load();
                    var username = !isEmpty(data.data.nickname) ? data.data.nickname : !isEmpty(data.data.username) ? data.data.username : "未设置";
                    $("#username_span").text(username);
                    $("#level_span").text('Lv ' + data.data.level);
                    $("#progress").val(data.data.useCapacityNum);
                    $("#progress").attr("max", data.data.capacityNum);
                    if (data.data.useCapacityNum / data.data.capacityNum > 0.9) {
                        $("#progress").addClass("progress_full");
                    } else {
                        $("#progress").removeClass("progress_full");
                    }
                    $("#useCapacity_span").text(data.data.useCapacity);
                    $("#capacity_span").text(data.data.capacity);
                    $("#freeDownloadCountSpan").text(data.data.availableDownloadCapacity);
                    if (data.data.acceptAgree != 1) {
                        $("#openAgreementButton").click();
                        timeout();
                    }
                } else {
                    toastr.options = {
                        "positionClass": "toast-top-title-center",
                    };
                    toastr.error(data.msg);
                }
            },
            error: function (data) {
                toastr.options = {
                    "positionClass": "toast-top-title-center",
                };
                toastr.error("获取用户信息失败");
            },
            type: "post"
        });
    }

    getList(null, null, null);
    getCurrentUser();

    $(".list_files").click(function () {
        var parentid = $(this).attr("value");
        getList(parentid, null, null);
    })

    $(".list_file_classify").click(function () {
        var classify = $(this).attr("id");
        getListClassify(classify);
    })

    $("#recycle").click(function () {
        getListRecycle();
    })

    function getSelectItem() {
        var elementArray = [0];
        var itemCount = 0;

        $(".fileCheckBox").each(function (index, element) {
            if ($(element).prop("checked")) {
                elementArray[itemCount++] = $(element).val();
            }
        });

        $(".folderCheckBox").each(function (index, element) {
            if ($(element).prop("checked")) {
                elementArray[itemCount++] = $(element).val();
            }
        });
        if (itemCount > 0) {
            return elementArray;
        }
        return null;
    }

    function getSelectItemOfFile() {
        var elementArray = [0];
        var itemCount = 0;

        $(".fileCheckBox").each(function (index, element) {
            if ($(element).prop("checked")) {
                elementArray[itemCount++] = $(element).val();
            }
        });
        if (itemCount > 0) {
            return elementArray;
        }
        return null;
    }

    $("#getBig").click(function () {
        toastr.options = {
            "positionClass": "toast-top-title-center",
        };
        toastr.info("暂未开放，敬请期待！");
    })

    $("#update_user_head").change(function () {
        upload();
    });

    function upload() {
        if ($("#update_user_head").val() == '') {
            return;
        }
        var formData = new FormData();
        formData.append('headPhoto', document.getElementById('update_user_head').files[0]);
        $.ajax({
            async: true,
            url: "/file/browse/update/head",
            data: formData,
            contentType: false,
            processData: false,
            dataType: "json",
            success: function (data) {
                if (data.status == 200) {
                    $("#user_head").attr("src", data.data);
                    $("#user_head").attr("src", data.data).load();
                    toastr.options = {
                        "positionClass": "toast-top-title-center",
                    };
                    toastr.info("更新头像成功");
                } else {
                    toastr.options = {
                        "positionClass": "toast-top-title-center",
                    };
                    toastr.error(data.msg);
                }

            },
            error: function (data) {
                toastr.options = {
                    "positionClass": "toast-top-title-center",
                };
                toastr.error("更新头像失败");
            },
            type: "post"
        });
    }


    $("#delete_remark").click(function () {
        $("#remark").val("");
        $("#select_file").val("")
    });
    $("#file_submit").click(function () {
        if ($("#select_file").val().trim() == "") {
            toastr.options = {
                "positionClass": "toast-top-title-center",
            };
            toastr.error("未选择文件");
            return false
        }
        var remarkText = $("#remark").val().trim();
        if (remarkText.length > 20) {
            remarkText = remarkText.substr(0, 20);
            $("#remark").val(remarkText)
        }
        var useCapacity = parseInt($("#useCapacity").text());
        var maxCapacity = parseInt($("#maxCapacity").text());
        if (useCapacity >= maxCapacity) {
            toastr.options = {
                "positionClass": "toast-top-title-center",
            };
            toastr.error("您的存储空间已满，清理空间后继续");
            $("#remark").val("");
            $("#select_file").val("");
            return false
        }
    });

    $("#fuzzySelect").click(function () {
        $("#selectAll").attr("checked", false);
        clear_choice();
        $("#remove_add_row").click();
        var match = $("#matchCode").val().trim();
        if (isEmpty(match)) {
            toastr.options = {
                "positionClass": "toast-top-title-center",
            };
            toastr.error("搜索关键字不可以为空");
            return;
        }
        fileItemsTable.recycle = 0;
        $.ajax({
            async: true,
            url: "/file/browse/search",
            data: {
                matchCode: match
            },
            dataType: "json",
            success: function (data) {
                if (data.status == 200) {
                    toastr.options = {
                        "positionClass": "toast-top-title-center",
                    };
                    toastr.info(data.msg);
                    fileItemsTable.setFiles(data.data);
                    absolutePath.absolutePath = null;
                    choice_tatal();
                    clear_choice();
                    clear_left_menu_back();
                } else {
                    toastr.options = {
                        "positionClass": "toast-top-title-center",
                    };
                    toastr.error(data.msg);
                }
            },
            error: function (data) {
                toastr.options = {
                    "positionClass": "toast-top-title-center",
                };
                toastr.error("文件搜索失败");
            },
            type: "post"
        });
    });

    var clear_choice = function () {
        $(".fileCheckBox").each(function (index, element) {
            $(element).prop("checked", false);

        });
        $(".folderCheckBox").each(function (index, element) {
            $(element).prop("checked", false);
        });
        $("#selectAll").attr("checked", false);
        $("#choice_total").hide();
    }

    function choice_tatal() {
        var isSelectAll = false;
        var choice_number = 0;
        $(".fileCheckBox").each(function (index, element) {
            if ($(element).prop("checked")) {
                choice_number = choice_number + 1;
                isSelectAll = true;
            } else {
                isSelectAll = false;
            }
        });
        $(".folderCheckBox").each(function (index, element) {
            if ($(element).prop("checked")) {
                choice_number = choice_number + 1;
                isSelectAll = true;
            } else {
                isSelectAll = false;
            }
        });
        if (choice_number > 0) {
            $("#choice_total").show();
            $("#choice_number").text(choice_number);
        } else {
            $("#choice_total").hide();
        }
        $("#selectAll").prop("checked", isSelectAll);
    }

    function choice_tatal_share() {
        var isSelectAll = false;
        var choice_number = 0;
        $(".shareCheckBox").each(function (index, element) {
            if ($(element).prop("checked")) {
                choice_number = choice_number + 1;
                isSelectAll = true;
            } else {
                isSelectAll = false;
            }
        });
        if (choice_number > 0) {
            $("#choice_total_share").show();
            $("#choice_number_share").text(choice_number);
        } else {
            $("#choice_total_share").hide();
        }
        $("#select_all_share").prop("checked", isSelectAll);
    }

    $("#selectAll").click(function () {
        var status = $(this).prop("checked");
        $(".fileCheckBox").each(function (index, element) {
            $(element).prop("checked", status)
        });
        $(".folderCheckBox").each(function (index, element) {
            $(element).prop("checked", status)
        });

        if (status) {
            $(".fileItem").each(function (index, element) {
                $(element).css("background", "rgb(245, 251, 255)")
            });
            $(".folderItem").each(function (index, element) {
                $(element).css("background", "rgb(245, 251, 255)")
            })
        } else {
            $(".fileItem").each(function (index, element) {
                $(element).css("background", "")
            });
            $(".folderItem").each(function (index, element) {
                $(element).css("background", "")
            })
        }
        choice_tatal();
    });

    $("#restore").click(function () {

        var fileIdArray = getSelectItem();
        if (fileIdArray == null) {
            toastr.options = {
                "positionClass": "toast-top-title-center",
            };
            toastr.error("无选中项");
            return
        }
        if (!confirm("是否放回选中项目?")) {
            return
        }

        $.ajax({
            async: true,
            data: {
                fileid: fileIdArray
            },
            dataType: "json",
            url: "/file/browse/restore",
            traditional: true,
            success: function (data) {
                if (data.status == 200) {
                    toastr.options = {
                        "positionClass": "toast-top-title-center",
                    };
                    toastr.info(data.msg);
                    getListRecycle();
                } else {
                    toastr.options = {
                        "positionClass": "toast-top-title-center",
                    };
                    toastr.error(data.msg);
                }
            },
            error: function () {
                toastr.options = {
                    "positionClass": "toast-top-title-center",
                };
                toastr.error("放回失败");
            },
            type: "post"
        })
    });

    $("#delete").click(function () {

        var fileIdArray = getSelectItem();
        if (fileIdArray == null) {
            toastr.options = {
                "positionClass": "toast-top-title-center",
            };
            toastr.error("无选中项");
            return
        }
        if (!confirm("彻底移除选中项目?")) {
            return
        }

        $.ajax({
            async: true,
            data: {
                fileid: fileIdArray
            },
            dataType: "json",
            url: "/file/browse/remove",
            traditional: true,
            success: function (data) {
                if (data.status == 200) {
                    toastr.options = {
                        "positionClass": "toast-top-title-center",
                    };
                    toastr.info(data.msg);
                    getListRecycle();
                    getCurrentUser();
                } else {
                    toastr.options = {
                        "positionClass": "toast-top-title-center",
                    };
                    toastr.error(data.msg);
                }
            },
            error: function () {
                toastr.options = {
                    "positionClass": "toast-top-title-center",
                };
                toastr.error("移除失败");
            },
            type: "post"
        })
        getCurrentUser();
    });

    $("#flushRecycle").click(function () {
        if (fileItemsTable.files == null || fileItemsTable.files.length == 0) {
            toastr.options = {
                "positionClass": "toast-top-title-center",
            };
            toastr.info("废纸篓已经很干净了哦");
            return;
        }
        if (confirm("是否倾倒废纸篓")) {
            $.ajax({
                async: true,
                dataType: "json",
                url: "/file/browse/recycle/flush",
                traditional: true,
                success: function (data) {
                    if (data.status == 200) {
                        toastr.options = {
                            "positionClass": "toast-top-title-center",
                        };
                        toastr.info(data.msg);
                        getListRecycle();
                        getCurrentUser();
                    } else {
                        toastr.options = {
                            "positionClass": "toast-top-title-center",
                        };
                        toastr.error(data.msg);
                    }
                },
                error: function () {
                    toastr.options = {
                        "positionClass": "toast-top-title-center",
                    };
                    toastr.error("移除失败");
                },
                type: "post"
            })
        }
    });

    $("#downloadSelectAll").click(function () {

        var fileIdArray = getSelectItemOfFile();
        if (fileIdArray == null) {
            toastr.options = {
                "positionClass": "toast-top-title-center",
            };
            toastr.error("无选中文件项");
            return
        }

        if (!confirm("是否下载选中项?")) {
            return
        }

        for (let i = 0; i < fileIdArray.length; i++) {
            $.ajax({
                async: true,
                data: {fileid: fileIdArray[i]},
                dataType: "json",
                url: "/file/browse/download",
                success: function (data) {
                    if (data.status == 200) {
                        try {
                            var elemIF = document.createElement("iframe");
                            elemIF.src = data.data;
                            elemIF.style.display = "none";
                            document.body.appendChild(elemIF);
                            getCurrentUser();
                        } catch (e) {
                            toastr.options = {
                                "positionClass": "toast-top-title-center",
                            };
                            toastr.error("下载异常！");
                        }
                    } else {
                        toastr.options = {
                            "positionClass": "toast-top-title-center",
                        };
                        toastr.error(data.msg);
                    }
                },
                error: function () {
                    toastr.options = {
                        "positionClass": "toast-top-title-center",
                    };
                    toastr.error("下载失败");
                },
                type: "post"
            })
        }
    });


    $("#deleteSelectAll").click(function () {

        var fileIdArray = getSelectItem();
        if (fileIdArray == null) {
            toastr.options = {
                "positionClass": "toast-top-title-center",
            };
            toastr.error("无选中项");
            return
        }
        if (!confirm("是否将选中项移至废纸篓?")) {
            return
        }

        $.ajax({
            async: true,
            data: {
                fileid: fileIdArray
            },
            dataType: "json",
            url: "/file/browse/recycle",
            traditional: true,
            success: function (data) {
                if (data.status == 200) {
                    toastr.options = {
                        "positionClass": "toast-top-title-center",
                    };
                    toastr.info(data.msg);
                    getList();
                } else {
                    toastr.options = {
                        "positionClass": "toast-top-title-center",
                    };
                    toastr.error(data.msg);
                }
            },
            error: function () {
                toastr.options = {
                    "positionClass": "toast-top-title-center",
                };
                toastr.error("删除失败");
            },
            type: "post"
        })
    });

    var share_file_id;
    var default_qr_img_url = "https://www.xcloud.show/static/img/share_default.png";

    function open_share_form(fileId, FileName) {
        $("#upload_form").hide();
        $("#update_form").hide();
        $("#move_form").hide();
        $("#archive_form").hide();
        share_file_id = fileId;
        var fileNameEXName = FileName;
        var fileName = fileNameEXName.substr(0, fileNameEXName.lastIndexOf("."));
        if (fileName.length > 10) {
            fileNameEXName = fileName.substr(0, 10) + '···' + fileNameEXName.substr(fileNameEXName.lastIndexOf("."));
        }
        $("#share_file_name_span").text('-' + fileNameEXName);

        $("#share_link_input").val("");
        $("#share_qr_img").attr("src", default_qr_img_url);
        $("#share_qr_img").attr("src", default_qr_img_url).load();

        $.ajax({
            async: true,
            data: {
                fileid: share_file_id,
            },
            dataType: "json",
            url: "/share/info",
            success: function (data) {
                if (data.status == 200) {

                    if (data.data.qrLinkStatus == 1) {
                        if (data.data.qrLink != null) {
                            toastr.options = {
                                "positionClass": "toast-top-title-center",
                            };
                            toastr.info("获取文件分享二维码成功");
                            $("#share_qr_img").attr("src", data.data.qrLink);
                            $("#share_qr_img").attr("src", data.data.qrLink).load();
                        }
                    } else if (data.data.qrLinkStatus == -1) {
                        toastr.options = {
                            "positionClass": "toast-top-title-center",
                        };
                        toastr.info("文件分享二维码已失效，请重新生成");
                    }

                    if (data.data.shareLinkStatus == 1) {
                        if (data.data.shareLink != null) {
                            toastr.options = {
                                "positionClass": "toast-top-title-center",
                            };
                            toastr.info("获取文件分享链接成功");
                            $("#share_link_input").val(data.data.shareLink);
                        }
                    } else if (data.data.shareLinkStatus == -1) {
                        toastr.options = {
                            "positionClass": "toast-top-title-center",
                        };
                        toastr.info("文件分享链接已失效，请重新生成");
                    }
                } else {
                    if (data.msg != "此文件未分享") {
                        toastr.options = {
                            "positionClass": "toast-top-title-center",
                        };
                        toastr.error(data.msg);
                    }
                }
            },
            error: function () {
            },
            type: "post"
        })
        $("#share_form").show();
    }

    $("#close_share_form_a").click(function () {
        share_file_id = -1;
        $("#share_link_input").val("");
        $("#share_qr_img").attr("src", default_qr_img_url);
        $("#share_qr_img").attr("src", default_qr_img_url).load();
        $("#share_form").hide();
    })

    $("#create_share_link").click(function () {
        var timeout;
        if ($("#timeout3").prop("checked") == true) {
            timeout = 3;
        }
        if ($("#timeout7").prop("checked") == true) {
            timeout = 7;
        }
        $.ajax({
            async: true,
            data: {
                fileid: share_file_id,
                type: 1,
                timeout: timeout
            },
            dataType: "json",
            url: "/share/file",
            success: function (data) {
                if (data.status == 200) {
                    $("#share_link_input").val(data.data);
                    toastr.options = {
                        "positionClass": "toast-top-title-center",
                    };
                    toastr.info("生成分享链接成功");
                } else {
                    toastr.options = {
                        "positionClass": "toast-top-title-center",
                    };
                    toastr.error(data.msg);
                }
            },
            error: function () {
                toastr.options = {
                    "positionClass": "toast-top-title-center",
                };
                toastr.error("分享失败");
            },
            type: "post"
        })
    })
    $("#create_share_qr").click(function () {
        var timeout;
        if ($("#timeout3").prop("checked") == true) {
            timeout = 3;
        }
        if ($("#timeout7").prop("checked") == true) {
            timeout = 7;
        }
        $.ajax({
            async: true,
            data: {
                fileid: share_file_id,
                type: 0,
                timeout: timeout
            },
            dataType: "json",
            url: "/share/file",
            success: function (data) {
                if (data.status == 200) {
                    $("#share_qr_img").attr("src", data.data);
                    $("#share_qr_img").attr("src", data.data).load();
                    toastr.options = {
                        "positionClass": "toast-top-title-center",
                    };
                    toastr.info("生成分享二维码成功，扫一扫获取");
                } else {
                    toastr.options = {
                        "positionClass": "toast-top-title-center",
                    };
                    toastr.error(data.msg);
                }
            },
            error: function () {
                toastr.options = {
                    "positionClass": "toast-top-title-center",
                };
                toastr.error("分享失败");
            },
            type: "post"
        })
    })

    var copyBtn = new ClipboardJS('.btn');
    copyBtn.on("success", function (e) {
        toastr.options = {
            "positionClass": "toast-top-title-center",
        };
        toastr.info("分享链接已复制至剪贴板");
        $("#share_link_input").blur();
    });
    copyBtn.on("error", function (e) {
        toastr.options = {
            "positionClass": "toast-top-title-center",
        };
        toastr.info("分享链接复制失败，请手动复制");
        $("#share_link_input").blur();
    });
    $("#share_link_input").focus(function () {
        if (!isEmpty($(this).val())) {
            $("#copy_content").click();
        }
    });

    initFileInput("asy_bs_upload");

    $("#asy_bs_upload").on("fileuploaded", function (event, data, previewId, index) {
        var result = data.response;
        if (result.status == 200) {
            toastr.options = {
                "positionClass": "toast-top-title-center",
            };
            var number = parseInt(index) + 1;
            toastr.info('第 ' + number + ' 个文件' + result.msg + '，刷新页面或关闭上传窗口以展示');
            getList();
            getCurrentUser();
        } else {
            toastr.options = {
                "positionClass": "toast-top-title-center",
            };
            toastr.error(result.msg);
        }
    });

    $("#asy_bs_upload").on("filebatchuploadsuccess", function (event, data, previewId, index) {
        var result = data.response;
        if (result.status == 200) {
            toastr.options = {
                "positionClass": "toast-top-title-center",
            };
            toastr.info(result.msg + '，刷新页面或关闭上传窗口以展示');
            getList();
            getCurrentUser();
        } else {
            toastr.options = {
                "positionClass": "toast-top-title-center",
            };
            toastr.error(result.msg);
        }
    });

    $("#open_asy_upload_file").click(function () {
        $("#upload_form").show();
        $("#share_form").hide();
        $("#update_form").hide();
        $("#move_form").hide();
        $("#archive_form").hide();
        var value = $("#progress").val();
        var max = $("#progress").attr("max");
        var result = value / max;
        if (result > 0.9) {
            toastr.options = {
                "positionClass": "toast-top-title-center",
            };
            toastr.warning("已用容量超过90%，请及时清理!");
        }
    });

    $("#close_upload_form_a").click(function () {
        $("#upload_form").hide();
    });

    // $(".update_A").mouseout(function() {
    //     $("#update_form").hide();
    // })

    var update_fileid;
    var update_fileName;
    var update_fun = function (fileId) {
        $("#upload_form").hide();
        $("#share_form").hide();
        $("#move_form").hide();
        $("#archive_form").hide();

        update_fileid = fileId;
        $.ajax({
            async: true,
            data: {fileid: update_fileid},
            dataType: "json",
            url: "/file/browse/detail",
            success: function (data) {
                if (data.status == 200) {
                    if (data.data.folder == 0) {
                        $("#update_name_span").val(data.data.fileName.substr(0, data.data.fileName.lastIndexOf(".")));
                        $("#update_type_span").val(data.data.fileName.substr(data.data.fileName.lastIndexOf(".") + 1));
                    } else {
                        $("#update_name_span").val(data.data.fileName);
                        $("#update_type_span").val("文件夹");
                    }
                    $("#update_size_span").val(data.data.folder == 1 ? "-" : data.data.fileSize);
                    $("#update_time_span").val(data.data.uploadTime);
                    update_fileName = data.data.fileName;
                } else {
                    toastr.options = {
                        "positionClass": "toast-top-title-center",
                    };
                    toastr.error(data.msg);
                }
            },
            error: function () {
                toastr.options = {
                    "positionClass": "toast-top-title-center",
                };
                toastr.error("获取文件信息失败");
            },
            type: "post"
        })

        $("#update_form").show();
    }
    $("#close_update_form_a").click(function () {
        $("#update_form").hide();
    })

    $("#update_detail_submit").click(function () {
        var newFileName = $("#update_name_span").val();
        if (isEmpty(newFileName)) {
            toastr.options = {
                "positionClass": "toast-top-title-center",
            };
            toastr.error("文件名不可为空");
            return;
        }
        if (update_fileName != newFileName) {
            $.ajax({
                async: true,
                data: {
                    fileid: update_fileid,
                    newFileName: newFileName
                },
                dataType: "json",
                url: "/file/browse/rename",
                success: function (data) {
                    if (data.status == 200) {
                        toastr.options = {
                            "positionClass": "toast-top-title-center",
                        };
                        toastr.info(data.msg);
                        update_fileName = newFileName;
                        getList();
                    } else {
                        toastr.options = {
                            "positionClass": "toast-top-title-center",
                        };
                        toastr.error(data.msg);
                    }
                },
                error: function () {
                    toastr.options = {
                        "positionClass": "toast-top-title-center",
                    };
                    toastr.error("文件重命名失败");
                },
                type: "post"
            })
        } else {
            toastr.options = {
                "positionClass": "toast-top-title-center",
            };
            toastr.error("未做任何修改");
        }
    })

    $("#update_name_span").keydown(function (event) {
        if (event.keyCode == 13) {
            $("#update_detail_submit").click();
        }
    })

    $("#open_move_form").click(function () {
        $("#share_form").hide();
        $("#upload_form").hide();
        $("#update_form").hide();
        $("#archive_form").hide();
        $("#move_form").show();
        init_move_form(move_parentId);
    })
    $("#close_move_form_a").click(function () {
        $("#move_form").hide();
    })

    $("#move_button").click(function () {
        var fileIdArray = getSelectItem();
        if (fileIdArray == null) {
            toastr.options = {
                "positionClass": "toast-top-title-center",
            };
            toastr.error("无选中项");
            return
        }
        if (!confirm("确定要移动吗?")) {
            return
        }
        $.ajax({
            async: true,
            data: {
                parentId: move_parentId,
                fileId: fileIdArray
            },
            dataType: "json",
            traditional: true,
            url: "/file/browse/move",
            success: function (data) {
                if (data.status == 200) {
                    toastr.options = {
                        "positionClass": "toast-top-title-center",
                    };
                    toastr.info(data.msg);
                    getList();
                    getCurrentUser();
                    clear_choice();
                } else {
                    toastr.options = {
                        "positionClass": "toast-top-title-center",
                    };
                    toastr.error(data.msg);
                }
            },
            error: function () {
                toastr.options = {
                    "positionClass": "toast-top-title-center",
                };
                toastr.error("文件移动失败");
            },
            type: "post"
        })
        $(".fileItem").each(function (index, element) {
            $(element).css("background", "")
        });
        $(".folderItem").each(function (index, element) {
            $(element).css("background", "")
        })
    });

    function init_move_form(parentId) {
        $.ajax({
            async: true,
            data: {parentId: parentId},
            dataType: "json",
            url: "/file/browse/list/lower",
            success: function (data) {
                if (data.status == 200) {
                    flush_move_form(data.data.parentId, data.data.folderVo, data.data.absolutePath)
                } else {
                    toastr.options = {
                        "positionClass": "toast-top-title-center",
                    };
                    toastr.error(data.msg);
                }
            },
            error: function () {
                toastr.options = {
                    "positionClass": "toast-top-title-center",
                };
                toastr.error("获取文件夹列表失败");
            },
            type: "post"
        })
    }

    $("#move_form_open_absolutePath_home").click(function () {
        lastFolderName = "根目录";
        init_move_form(-1);
    })

    function flush_move_form(parentId, folderVo, absolutePath) {
        move_parentId = parentId;
        $(".folder_record_tr").remove();
        $(".move_file_absolute").remove();
        $.each(folderVo, function (index, element) {
            var record = "<tr class='" + "folder_record_tr" + "'><td style='text-align: left; white-space: nowrap;text-overflow: ellipsis;overflow: hidden;'><a href='javascript:void(0)' id='" + element.id + "' class='move_form_open_folder_a' style='margin-left: 20px'>" + element.fileName + "</a></td><td>" + element.uploadTime + "</td></tr>";
            $("#move_form_table").append(record);
        })
        if (folderVo.length == 0) {
            var record = "<tr class='" + "folder_record_tr" + "'><td><span class='glyphicon glyphicon-exclamation-sign' style='font-size: 14px; color: rgb(117, 131, 186);'></span>&nbsp;&nbsp;无文件夹</td></tr>";
            $("#move_form_table").append(record);
        }

        $.each(absolutePath, function (index, element) {
            var folderName = element.folderName;
            if (folderName.length > 10) {
                folderName = folderName.substr(0, 10) + '...';
            }
            var record;
            if (index == absolutePath.length - 1) {
                record = "<li class='divider move_file_absolute'><a id='" + element.parentid + "' class='move_form_open_absolutePath' style='text-decoration:none;'><span style='color:rgb(185,185,185)'>" + folderName + "</span></a></li>";
                lastFolderName = folderName;
            } else {
                record = "<li class='divider move_file_absolute'><a id='" + element.parentid + "' class='move_form_open_absolutePath' style='text-decoration:none;'><span>" + folderName + "</span></a></li>"
            }
            $("#move_file_absolutePath").append(record);
        })

        $(".move_form_open_folder_a").click(function () {
            init_move_form($(this).attr("id"));
        })

        $(".move_form_open_absolutePath").click(function () {
            init_move_form($(this).attr("id"));
        })
    }

    $("#copy_button").click(function () {
        var fileIdArray = getSelectItem();
        if (fileIdArray == null) {
            toastr.options = {
                "positionClass": "toast-top-title-center",
            };
            toastr.error("无选中项");
            return
        }
        if (!confirm("确定要复制吗?")) {
            return
        }
        $.ajax({
            async: true,
            data: {
                parentId: move_parentId,
                fileId: fileIdArray
            },
            dataType: "json",
            traditional: true,
            url: "/file/browse/copy",
            success: function (data) {
                if (data.status == 200) {
                    toastr.options = {
                        "positionClass": "toast-top-title-center",
                    };
                    toastr.info(data.msg);
                    getList();
                    getCurrentUser();
                    clear_choice();
                } else {
                    toastr.options = {
                        "positionClass": "toast-top-title-center",
                    };
                    toastr.error(data.msg);
                }
            },
            error: function () {
                toastr.options = {
                    "positionClass": "toast-top-title-center",
                };
                toastr.error("文件复制失败");
            },
            type: "post"
        })
        $(".fileItem").each(function (index, element) {
            $(element).css("background", "")
        });
        $(".folderItem").each(function (index, element) {
            $(element).css("background", "")
        })
    });

    $("#open_archive_button").click(function () {
        $("#move_form").hide();
        $("#archive_form_absolute").text('-' + lastFolderName);
        $("#archive_qr_img").attr("src", "https://www.xcloud.show/static/img/share_default.png");
        $("#choice_share_ar").val("");
        $("#archive_form").show();
    })
    $("#close_archive_form_a").click(function () {
        $("#archive_form").hide();
        $("#open_move_form").click();
    })
    $("#show_share_qr").click(function () {
        $("#choice_share_ar").click()
    })
    $("#choice_share_ar").change(function () {
        // 构造一个文件渲染对象
        var reader = new FileReader();
        // 得到文件列表数组
        fileObj = $(this)[0].files[0];
        // 拿到文件数据
        reader.readAsDataURL(fileObj);

        reader.onload = function () {
            // 获取文件信息
            imgData = reader.result;
            // 指定位置显示图片
            $("#archive_qr_img").attr("src", imgData);

        };
    })
    $("#archive").click(function () {
        var archive_type;
        if ($("#share_type_0").prop("checked") == true) {
            archive_type = 0;
        }
        if ($("#share_type_1").prop("checked") == true) {
            archive_type = 1;
        }
        if (archive_type == 0) {
            if ($("#choice_share_ar").val() == '') {
                toastr.options = {
                    "positionClass": "toast-top-title-center",
                };
                toastr.error("请选择文件分享二维码");
                return;
            }
            var formData = new FormData();
            formData.append('qr', document.getElementById('choice_share_ar').files[0]);
            formData.append('parentId', move_parentId);
            formData.append('type', "0");
            $.ajax({
                async: true,
                url: "/file/browse/archive",
                data: formData,
                contentType: false,
                processData: false,
                dataType: "json",
                success: function (data) {
                    if (data.status == 200) {
                        toastr.options = {
                            "positionClass": "toast-top-title-center",
                        };
                        toastr.info("文件转存成功，" + data.msg);
                        getList();
                        getCurrentUser();
                        clear_choice();
                    } else {
                        toastr.options = {
                            "positionClass": "toast-top-title-center",
                        };
                        toastr.error(data.msg);
                    }
                },
                error: function (data) {
                    toastr.options = {
                        "positionClass": "toast-top-title-center",
                    };
                    toastr.error("文件转存失败");
                },
                type: "post"
            });
        } else if (archive_type == 1) {
            var archiveUrl = $("#archive_share_url").val();
            if (isEmpty(archiveUrl)) {
                toastr.options = {
                    "positionClass": "toast-top-title-center",
                };
                toastr.error("请输入分享链接");
                return;
            }
            $.ajax({
                async: true,
                data: {
                    url: archiveUrl,
                    parentId: move_parentId,
                    type: 1
                },
                dataType: "json",
                url: "/file/browse/archive",
                success: function (data) {
                    if (data.status == 200) {
                        toastr.options = {
                            "positionClass": "toast-top-title-center",
                        };
                        getList();
                        getCurrentUser();
                        clear_choice();
                    } else {
                        toastr.options = {
                            "positionClass": "toast-top-title-center",
                        };
                        toastr.error(data.msg);
                    }
                },
                error: function () {
                    toastr.options = {
                        "positionClass": "toast-top-title-center",
                    };
                    toastr.error("文件转存失败");
                },
                type: "post"
            })
        }
    })

    var add_flag = true;

    var add_row = function () {
        var name = $("#folderName").val().trim();
        if (!isEmpty(name) && name.length < 32) {
            $.ajax({
                async: true,
                data: {foldername: name},
                dataType: "json",
                url: "/file/browse/createfolder",
                success: function (data) {
                    if (data.status == 200) {
                        toastr.options = {
                            "positionClass": "toast-top-title-center",
                        };
                        remove_add_row();
                        getList();
                    } else {
                        toastr.options = {
                            "positionClass": "toast-top-title-center",
                        };
                        toastr.error(data.msg);
                    }
                },
                error: function () {
                    toastr.options = {
                        "positionClass": "toast-top-title-center",
                    };
                    toastr.error("新建文件夹失败");
                },
                type: "post"
            })
        } else {
            toastr.options = {
                "positionClass": "toast-top-title-center",
            };
            toastr.error("文件夹名格式有误！格式：32位内字符");
        }
    }

    var remove_add_row = function () {
        $("#add_row").remove();
        add_flag = true;
    }

    $("#addFolderButton").click(function () {
        if (add_flag) {
            var row = "<tr id='add_row'>" +
                "<td colspan='2'>" +
                "<input id='folderName' type='text' name='foldername' placeholder='新建文件夹'>" +
                "<a id='add_row' href='javascript:void(0)' style='margin-left: 5px'>" +
                "<span><img src='https://www.xcloud.show/static/img/xcloud_front/enter.png' style='height: 22px'></span>" +
                "</a>" +
                "<a id='remove_add_row' href='javascript:void(0)' style='margin-left: 1px'>" +
                "<span><img src='https://www.xcloud.show/static/img/xcloud_front/esc.png' style='height: 20px'></span>" +
                "</a>" +
                "</td>" +
                "<td><span style='font-size: 10px; color: rgb(146, 146, 146)'>-</span></td>" +
                "<td><span style='font-size: 10px; color: rgb(146, 146, 146)'>-</span></td>" +
                "<td colspan='2'></td>" +
                "<td style='width: 9%'></td>" +
                "</tr>";
            $("#file_table tbody").prepend(row);
            add_flag = false;
            $("#folderName").click(function () {
                return false;
            })
            $("#folderName").blur(function () {
                return false;
            })
            $("#folderName").focus(function () {
                return false;
            })
            $("#folderName").bind('keypress', function (event) {
                if (event.keyCode == "13") {
                    $("#add_row").click();
                }
            });
            $("#remove_add_row").click(function () {
                event.stopPropagation();
                remove_add_row();
            })
            $("#add_row").click(function () {
                event.stopPropagation();
                add_row();
            })
        }
        $("#folderName").focus();
    })

    $("#sort_type").change(function () {
        var flag = $(this).val()
        getList(null, flag, 0);
    })

    function add_share_timeout(shareId, type) {
        $.ajax({
            async: true,
            data: {
                shareId: shareId,
                type: type
            },
            dataType: "json",
            url: "/share/extend",
            success: function (data) {
                if (data.status == 200) {
                    toastr.options = {
                        "positionClass": "toast-top-title-center",
                    };
                    toastr.info(data.msg);
                    flush_share_list_table();
                    clear_choice_share();
                } else {
                    toastr.options = {
                        "positionClass": "toast-top-title-center",
                    };
                    toastr.error(data.msg);
                    clear_choice_share();
                }
            },
            error: function () {
                toastr.options = {
                    "positionClass": "toast-top-title-center",
                };
                toastr.error("分享移除失败");
            },
            type: "post"
        })
    }

    var table = new Vue({
        el: "#share_table",
        data: {
            shares: []
        },
        methods: {
            choice_tatal_share() {
                choice_tatal_share()
            },
            getClass() {
                return "shareCheckBox"
            },
            addTimeout(shareId, type) {
                add_share_timeout(shareId, type)
            }
        }
    })

    $("#select_all_share").click(function () {
        var status = $(this).prop("checked");
        $(".shareCheckBox").each(function (index, element) {
            $(element).prop("checked", status)
        });
        choice_tatal_share();
    })

    function flush_share_list_table() {
        $.ajax({
            async: true,
            url: "/share/list",
            contentType: false,
            processData: false,
            dataType: "json",
            success: function (data) {
                if (data.status == 200) {
                    toastr.options = {
                        "positionClass": "toast-top-title-center",
                    };
                    toastr.info("获取文件分享列表成功");
                    table.shares = data.data;
                } else {
                    toastr.options = {
                        "positionClass": "toast-top-title-center",
                    };
                    toastr.error(data.msg);
                }
            },
            error: function (data) {
                toastr.options = {
                    "positionClass": "toast-top-title-center",
                };
                toastr.error("获取文件分享列表失败");
            },
            type: "post"
        });
    }

    $("#share").click(function () {
        flush_share_list_table();
    })

    var clear_choice_share = function () {
        $(".shareCheckBox").each(function (index, element) {
            $(element).prop("checked", false);

        });
        $("#select_all_share").attr("checked", false);
        $("#choice_total_share").hide();
    }

    $("#remove_select_share").click(function () {

        var elementArray = [0];
        var itemCount = 0;

        $(".shareCheckBox").each(function (index, element) {
            if ($(element).prop("checked")) {
                elementArray[itemCount++] = $(element).val();
            }
        });
        if (itemCount == 0) {
            toastr.options = {
                "positionClass": "toast-top-title-center",
            };
            toastr.error("无选中分享项");
            return
        }
        if (!confirm("是否将选中的分享彻底移除?")) {
            return
        }

        $.ajax({
            async: true,
            data: {
                shareId: elementArray
            },
            dataType: "json",
            url: "/share/remove",
            traditional: true,
            success: function (data) {
                if (data.status == 200) {
                    toastr.options = {
                        "positionClass": "toast-top-title-center",
                    };
                    toastr.info(data.msg);
                    flush_share_list_table();
                    clear_choice_share();
                } else {
                    toastr.options = {
                        "positionClass": "toast-top-title-center",
                    };
                    toastr.error(data.msg);
                    clear_choice_share();
                }
            },
            error: function () {
                toastr.options = {
                    "positionClass": "toast-top-title-center",
                };
                toastr.error("分享移除失败");
            },
            type: "post"
        })
    });
})

function initFileInput(ctrlName) {
    var control = $('#' + ctrlName);
    control.fileinput({
        language: 'zh', //设置语言
        uploadUrl: "/file/browse/upload", //上传的地址
        //allowedFileExtensions: ['jpg', 'gif', 'png','exe'],//接收的文件后缀
        //uploadExtraData:{"id": 1, "fileName":'123.mp3'},
        uploadAsync: false, //默认异步上传
        showUpload: true, //是否显示上传按钮
        showRemove: true, //显示移除按钮
        showPreview: true, //是否显示预览
        showCaption: false, //是否显示标题
        browseClass: "btn btn-info", //按钮样式
        //dropZoneEnabled: true,//是否显示拖拽区域
        //minImageWidth: 50, //图片的最小宽度
        //minImageHeight: 50,//图片的最小高度
        //maxImageWidth: 50,//图片的最大宽度
        //maxImageHeight: 50,//图片的最大高度
        previewSettings: {
            image: {width: "105px", height: "105px"},
            html: {width: "105px", height: "105px"},
            text: {width: "105px", height: "105px"},
            video: {width: "105px", height: "105px"},
            audio: {width: "105px", height: "105px"},
            flash: {width: "105px", height: "105px"},
            object: {width: "105px", height: "105px"},
            other: {width: "105px", height: "105px"}
        },
        maxFileSize: 524288, //单位为kb，如果为0表示不限制文件大小
        //minFileCount: 0,
        maxFileCount: 128, //表示允许同时上传的最大文件个数
        enctype: 'multipart/form-data',
        validateInitialCount: true,
        previewFileIcon: "<i class='glyphicon glyphicon-question-sign'></i>",
        msgFilesTooMany: "选择上传的文件数量({n}) 超过允许的最大数值{m}！",
        layoutTemplates: {
            //actionDelete:'', //去除上传预览的缩略图中的删除图标
            //actionUpload:'',//去除上传预览缩略图中的上传图片；
            //actionZoom:''   //去除上传预览缩略图中的查看详情预览的缩略图标。
        },
    })
}
